import utils from '../utils'
import Dataset from '../dataset'
import io from './index.js'

/* @requires mapshaper-file-types */
function getOutputFormat(dataset, opts) {
  const outFile = opts.file || null
  const inFmt = dataset.info && dataset.info.input_formats && dataset.info.input_formats[0]
  let outFmt = null

  // if user has specified a format, use that
  if (opts.format) {
    return opts.format
  }

  // if an output filename is given, try to infer format from filename etc.
  if (outFile) {
    outFmt = inferOutputFormat(outFile, inFmt)
  } else if (inFmt) {
    outFmt = inFmt
  }

  if (outFmt == 'json' && Dataset.datasetHasGeometry(dataset)) {
    // special case: inferred output format is a json table (either because
    // the output file has a .json extension or because the input file was a
    // json table), but the output dataset contains shapes
    outFmt = 'geojson'
  }

  return outFmt || null
}
// Infer output format by considering file name and (optional) input format
function inferOutputFormat(file, inputFormat) {
  const ext = utils.getFileExtension(file).toLowerCase()
  let format = null
  if (ext == 'shp') {
    format = 'shapefile'
  } else if (ext == 'dbf') {
    format = 'dbf'
  } else if (ext == 'svg') {
    format = 'svg'
  } else if (/json$/.test(ext)) {
    format = 'geojson'
    if (ext == 'topojson' || inputFormat == 'topojson' && ext != 'geojson') {
      format = 'topojson'
    } else if (ext == 'json' && inputFormat == 'json') {
      // .json -> json table is not always the best inference...
      // additional logic should be applied downstream
      format = 'json' // JSON table
    }
  } else if (io.couldBeDsvFile(file)) {
    format = 'dsv'
  } else if (inputFormat) {
    format = inputFormat
  }
  return format
}
export default {
  getOutputFormat,
}